#!/usr/bin/python
import MySQLdb


def getHistory(rows):
	db = MySQLdb.connect(host="localhost", user="root", db="solar")
	cursor = db.cursor()
	cursor.execute("select * from solar_agg where time > TIMESTAMPADD(HOUR,-36,NOW()) order by time asc")
	rows = cursor.fetchmany(rows)
	cursor.close()
	db.close()
	return rows


rows = getHistory(100)

print "Content-Type: text/xml\n\n"
print "<chart>"
print "<chart_type><string>line</string></chart_type>"
print "<axis_category skip='12'/>"
print "<chart_data>"
print "<row>"
for row in rows:
	print "<string>%s</string>" % row[0].strftime("%I:%M%p")
print "</row>"
print "<row>"
print "<string>Watts</string>"
for row in rows:
	print "<number>%s</number>" % row[1]
print "</row>"
print "</chart_data>"
print "</chart>"

